package BandB.Tool.GPhoneManager.AppManager;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;

import BandB.Tool.GPhoneManager.R;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class UninstallerListAdapter extends ArrayAdapter<PackageInfo> {

	private static final String UNKNOWN = "Unkown";
	private static final String TAG = UninstallerListAdapter.class
			.getSimpleName();
	private LinkedList<PackInfoItem> mPackInfoList;
	private LayoutInflater mInflater;
	private PackageManager mPackageManager;
	private ArrayList<Boolean> mCheckedList;

	public UninstallerListAdapter(Context c) {
		super(c, 0);

		mPackInfoList = new LinkedList<PackInfoItem>();
		mInflater = LayoutInflater.from(c);
		mPackageManager = c.getPackageManager();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mPackInfoList.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.uninstaller_list_item,
					null);
		}
		ImageView icon = (ImageView) convertView.findViewById(R.id.app_icon);
		TextView name = (TextView) convertView.findViewById(R.id.app_name);
		TextView size = (TextView) convertView.findViewById(R.id.app_size);
		TextView date = (TextView) convertView.findViewById(R.id.app_date);
		final CheckBox checked = (CheckBox) convertView
				.findViewById(R.id.app_check_box);

		PackInfoItem info = getInfo(position);
		icon.setImageDrawable(info.mIcon);
		name.setText(info.mAppName);
		size.setText(info.mSize);
		date.setText(info.mDate);
		checked.setChecked(mCheckedList.get(position));

		return convertView;
	}

	/**
	 * This method will be called when packageInfo list is first fetched.
	 * 
	 * @param size
	 *            specify the count of the list which can be selected.
	 */
	public void setLoadingSize(int size) {
		// mCurrentSize = size;
		mCheckedList = new ArrayList<Boolean>(size);
		for (int i = 0; i < size; i++) {
			mCheckedList.add(i, false);
		}
	}

	public void addQueueData(Queue<PackageInfo> queue) {
		PackageInfo info = queue.poll();
		while (null != info) {
			ApplicationInfo applicationInfo = info.applicationInfo;
			File file = getSourceFile(applicationInfo);
			if (null != file) {
				PackInfoItem item = new PackInfoItem();
				long fileLength = file.length();
				long fileDate = file.lastModified();

				item.mPackageInfo = info;
				item.mAppName = applicationInfo.loadLabel(mPackageManager)
						.toString();
				item.mIcon = applicationInfo.loadIcon(mPackageManager);
				item.mSizeCompare = fileLength;
				item.mSize = formatSize(fileLength);
				item.mDateCompare = fileDate;
				item.mDate = formatDate(fileDate);

				mPackInfoList.add(item);
			}
			info = queue.poll();
		}
	}

	public void deleteItem(PackInfoItem item) {
		int index = mPackInfoList.indexOf(item);
		if (index != -1) {
			deleteInfo(index);
		} else {
			Log.e(TAG, "delete error,item not found.");
		}
	}

	private void deleteInfo(int index) {
		mPackInfoList.remove(index);
		mCheckedList.remove(index);
		// mCurrentSize--;
	}

	public void setCheckedAt(int position) {
		// TODO Auto-generated method stub
		boolean bool = mCheckedList.get(position);
		mCheckedList.set(position, !bool);
	}

	public PackInfoItem getInfo(int position) {
		return mPackInfoList.get(position);
	}

	public LinkedList<PackInfoItem> getCheckedList() {
		LinkedList<PackInfoItem> checked = new LinkedList<PackInfoItem>();
		int mCurrentSize = mPackInfoList.size();
		for (int i = 0; i < mCurrentSize; i++) {
			if (mCheckedList.get(i)) {
				checked.add(mPackInfoList.get(i));
			}
		}
		return checked;
	}

	private File getSourceFile(ApplicationInfo applicationInfo) {
		String path = applicationInfo.sourceDir;
		File file = new File(path);
		if (file.exists()) {
			return file;
		} else {
			return null;
		}
	}

	private String formatSize(long size) {
		if (size > 0) {
			String sizeString = String.valueOf(size >> 10);
			return sizeString + "KB";
		} else {
			return UNKNOWN;
		}
	}

	private String formatDate(long date) {
		if (date > 0) {
			Date newDate = new Date(date);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.format(newDate);
		} else {
			return UNKNOWN;
		}
	}

	/**
	 * Sort this adapter by size in UI thread.
	 */
	public void sortBySize() {
		Collections.sort(mPackInfoList, new Comparator<PackInfoItem>() {

			@Override
			public int compare(PackInfoItem object1, PackInfoItem object2) {
				// TODO Auto-generated method stub
				return object1.mSizeCompare.compareTo(object2.mSizeCompare);
			}
		});
	}

	/**
	 * Sort this adapter by name in UI thread.
	 */
	public void sortByName() {
		Collections.sort(mPackInfoList, new Comparator<PackInfoItem>() {

			@Override
			public int compare(PackInfoItem object1, PackInfoItem object2) {
				// TODO Auto-generated method stub

				return UninstallerListAppAdapter.sCollator.compare(
						object1.mAppName, object2.mAppName);
			}
		});
	}

	/**
	 * Sort this adapter by date in UI thread.
	 */
	public void sortByDate() {
		Collections.sort(mPackInfoList, new Comparator<PackInfoItem>() {
			@Override
			public int compare(PackInfoItem object1, PackInfoItem object2) {
				// TODO Auto-generated method stub

				return object1.mDateCompare.compareTo(object2.mDateCompare);
			}
		});
	}

}
